<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>我的好友</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
	<script type="text/javascript">
	function validate_form(thisform){
		with(thisform){
			if(message.value == ''){//判断留言是否为空
			alert('留言不能为空');
				return false;
			}
			return true;
		}
	}
	function clear_leave_message_view(containerID){
		var container = document.getElementById(containerID);
		if(container == null) {return;}
		else{
			container.innerHTML = "";
		}
	}
	function show_leave_message_view(containerID){
		var container = document.getElementById(containerID);
		if(container == null) {alert("没找到元素");return;}
		var belong_to = "1";//1标志不属于谁
		var handleAddr = "../handle/handle_message.php?to="+containerID+"&belong_to="+belong_to;
		container.innerHTML = 
		'<form action="'+handleAddr+'" method="post" onSubmit="return validate_form(this)">\
		<div>\
			<textarea cols=80 rows=5 name="message" id="message"></textarea>            <br/>\
			<div class="form-group">\
     			<div class="col-sm-offset-2 col-sm-1">\
        	 		<button type="submit" class="btn btn-default">提交	                    </button>\
      			</div>\
      			<div class="col-sm-offset-0 col-sm-1">\
          			<button type="button" class="btn btn-default" \
          				onClick="clear_leave_message_view('+"'"+containerID+"'"+')">收                           起</button>\
				</div>\
   			</div>\
		</div></form>';
	}
	
	</script>   
</head>
<body>
<?php require '../public/header.php'?>
<!-- 导航栏 -->
<ul class="nav nav-pills">
   <li><a href="home.php">主页</a></li>
   <li><a href="message_record.php">留言记录</a></li>
   <li class="active"><a href="#">我的好友</a></li>
   <li><a href="add_friends.php">添加好友</a></li>
   <li><a href="notice.php">我的消息</a></li>
</ul><hr/>
<!-- 留言记录 -->
<div class="panel panel-info"> 	
	<div class="panel-heading" id="message_record">
     	<h3 class="panel-title">好友列表</h3>
   </div>
	<div class="panel-body">
		<?php 
		if(isset($_SESSION['username'])){//已登陆
			$username=$_SESSION['username'];
			require '../public/conn.php';
			$result = mysql_query("SELECT * FROM friends WHERE                        friendA='$username' OR friendB='$username'");
			$friend = "";
			while($row = mysql_fetch_array($result))//读取好友记录并显示
  			{ 
				echo "<hr/><div>";
				if($row['friendA'] == $username){
					echo "<div style='width:40%;float:left;'>
							<a href='#'>".$row['friendB']."</a>
						  </div>";
					$friend = $row['friendB'];
				}else{
					echo "<div style='width:40%;float:left;'>
							<a href='#'>".$row['friendA']."</a>
						  </div>";
					$friend = $row['friendA'];
				}
				echo '<div style="width:40%;float:left;">
						<button type="button" class="btn btn-primary"
							name="';echo $friend;echo '"
								onClick="show_leave_message_view(this.name)">
				         	给他留言													                     	</button>
					  </div><br/><br/>';
			    echo '<div id="';echo $friend; echo '" name="$friend"></div>';
				echo "</div><br>";
 	 		}
			mysql_close($con);
		}else{		//未登陆
			echo "亲,先去<a href='../face/login-face.php'><span>登陆</span>                </a>吧！";
		}
	    ?>
   </div>
</div>
</body>
</html>